package org.zfes.snowier.cloud.zbss.pmp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.zfes.snowier.core.data.ComboboVo;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.exceptions.ServiceLogicalException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;

import com.google.common.collect.Lists;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpCloudMemberService;
import org.zfes.snowier.auth.biz.dto.AuthAccountDto;
import org.zfes.snowier.auth.biz.model.AuthUser;
import org.zfes.snowier.auth.biz.service.IAuthUserService;
import org.zfes.snowier.auth.shiro.util.ShiroMD5Util;
import org.zfes.snowier.cloud.zbss.pmp.dao.PmpCloudMemberMapper;
import org.zfes.snowier.cloud.zbss.pmp.dto.AccountDto;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpCloudMember;
import java.util.stream.Stream;
@Service
public class PmpCloudMemberServiceImpl implements IPmpCloudMemberService{
 	@Autowired
	private PmpCloudMemberMapper pmpCloudMemberMapper;
 	
 	@Autowired private IAuthUserService authUserService;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public Long createPmpCloudMember(AccountDto accountDto) {
		
		Long userId=authUserService.createAccount(AuthAccountDto.genWhithNoStep(accountDto.getAccount(), accountDto.getPassword(), accountDto.getRoleCode(), accountDto.getSalt()));
		//=========================
		PmpCloudMember pmpCloudMember=new PmpCloudMember();
		pmpCloudMember.setId(userId);
		pmpCloudMember.setStatus(Byte.valueOf("1"));
		pmpCloudMember.setPositionLevel(Byte.valueOf("1"));
		pmpCloudMember.setUserLogo(accountDto.getUserLogo());
		pmpCloudMember.setUserName(accountDto.getUserName());
		pmpCloudMember.setUserType(accountDto.getUserType());
		pmpCloudMember.setCreatetime(ZDateUtil.newDate());
		pmpCloudMember.setUpdatetime(ZDateUtil.newDate());
		
		ZBeanUtil.validateBean(pmpCloudMember);
		
		pmpCloudMemberMapper.insertSelective(pmpCloudMember);
		
		return userId;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<PmpCloudMember> loadPmpCloudMemberById(Long id) {
		if(id!=null){
            PmpCloudMember pmpCloudMember = pmpCloudMemberMapper.selectByPrimaryKey(id);
            return Optional.ofNullable(pmpCloudMember);
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updatePmpCloudMember(PmpCloudMember pmpCloudMember) {
		
		PmpCloudMember member = pmpCloudMemberMapper.selectByPrimaryKey(pmpCloudMember.getId());
		ZBeanUtil.copy(pmpCloudMember, member, true);
		
		member.setUpdatetime(ZDateUtil.newDate());
		ZBeanUtil.validateBean(member);
		pmpCloudMemberMapper.updateByPrimaryKeySelective(pmpCloudMember);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deletePmpCloudMember(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		
		PmpCloudMember member = loadPmpCloudMemberById(id).orElseThrow(()->new ServiceLogicalException("未查询到该数据"));
		ZAssert.notEquals(member.getStatus(), Byte.valueOf("-1"), "该员工已被删除");
		
		pmpCloudMemberMapper.deleteByPrimaryKey(id);
		authUserService.deleteAuthUser(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setPmpCloudMemberStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		pmpCloudMemberMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadPmpCloudMemberDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(pmpCloudMemberMapper.selectListCount(pm), pmpCloudMemberMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<PmpCloudMember> loadPmpCloudMemberByIds(List<Long> userIdList) {
		ZAssert.notNull(userIdList, "请选择数据");
        ZAssert.notEmpty(userIdList, "请选择数据");
		return pmpCloudMemberMapper.selectByUserIds(Byte.valueOf("1"),userIdList);
	}

	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void changepwd(String account, String newPassword, String oldpassword) {
		ZAssert.hasText(account, "未获取到用户信息");
		ZAssert.hasText(newPassword, "请输入新密码");
		ZAssert.hasText(oldpassword, "请再次输入旧密码");
		Optional<AuthUser> userOP=authUserService.loadByAccount(account);
		ZAssert.notEmpty(userOP, "未查询到用户信息");
		String salt=userOP.get().getSalt();
		
		newPassword=ShiroMD5Util.genSetp1Password(newPassword);
		newPassword=ShiroMD5Util.genSetp2Password(newPassword, salt);
		 
		  oldpassword=ShiroMD5Util.genSetp1Password(oldpassword);
		  oldpassword=ShiroMD5Util.genSetp2Password(oldpassword, salt);
		 
		authUserService.changePwd(userOP.get().getId(), newPassword, oldpassword);
	}
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void setpwd(String account, String newPassword) {
		ZAssert.hasText(account, "未获取到用户信息");
		ZAssert.hasText(newPassword, "请输入新密码");
		Optional<AuthUser> userOP=authUserService.loadByAccount(account);
		ZAssert.notEmpty(userOP, "未查询到用户信息");
		String salt=userOP.get().getSalt();
		
		newPassword=ShiroMD5Util.genSetp1Password(newPassword);
		newPassword=ShiroMD5Util.genSetp2Password(newPassword, salt);
		 
		authUserService.setAuthUserPwd(userOP.get().getId(), newPassword);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setUserLogo(Long id, String userLogo) {
		PmpCloudMember member = loadPmpCloudMemberById(id).orElseThrow(()->new ServiceLogicalException("未查询到该数据"));
		pmpCloudMemberMapper.updateUserLogo(id, userLogo);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setPositionLevel(Long id, Byte positionLevel) {
		PmpCloudMember member = loadPmpCloudMemberById(id).orElseThrow(()->new ServiceLogicalException("未查询到该数据"));
		if(!Stream.of(new Byte[] {1,2,3,4,5,6,7}).anyMatch(val->val.equals(positionLevel))) {
			ZAlert.serviceLogicalError("等级参数错误");
		}
		pmpCloudMemberMapper.updatePositionLevel(id, positionLevel);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setUserType(Long id, Byte userType) {
		
		if(!Stream.of(new Byte[] {1,2,3,4}).anyMatch(val->val.equals(userType))) {
			ZAlert.serviceLogicalError("成员类型参数错误");
		}
		PmpCloudMember member = loadPmpCloudMemberById(id).orElseThrow(()->new ServiceLogicalException("未查询到该数据"));
		pmpCloudMemberMapper.updateUserType(id, userType);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ComboboVo> loadComboboVo(boolean firstEmpty) {
		List<ComboboVo> voList=pmpCloudMemberMapper.selectComboboVo(Byte.valueOf("1"));
		if(firstEmpty) {
			voList.add(0, ComboboVo.newEmptyFirst());
		}
		return voList;
	}
	
}